private ["_box_idx","_not_found","_box_marker","_text_marker","_index","_box","_box_array","_box_type","_box_side","_marker_array","_last_fill","_marker_name","_box_extra","_box_extra_str"];

AMMO_BOXES = [];
AMMO_BOXES_INDEXER = [];

execVM "base\weapons\wpn_array.sqf";

waituntil {(count WEST_AMMO_BOX_CONTENT > 0) && (count EAST_AMMO_BOX_CONTENT > 0)};

_fnc_spawn_content = {
	private ["_box","_key","_content","_type","_count","_content_name","_extra"];
	_box = _this select 0;
	_key = _this select 1;
	_extra = _this select 2;
	_content_name = "";
	_content = nil;
	switch (toupper(_key)) do {
		case "EAST": { _content = EAST_AMMO_BOX_CONTENT };
		case "WEST": { _content = WEST_AMMO_BOX_CONTENT };
		default {
			_content_name = Format["%1_AMMO_BOX_CONTENT",toupper(_key)];
			if (isnil _content_name) then {
				_content_name = switch (d_own_side) do {
					case "EAST":{ "EAST_AMMO_BOX_CONTENT" };
					default { "WEST_AMMO_BOX_CONTENT" };
				};
			};
			call compile Format["_content = %1;",_content_name];
		};
	};
	
	if (!isnil "_content") then {
		if (count _extra > 0) then {
			_content = [_content, _extra] call XfArrayPushStack;
		};
		if (count _content > 0) then {
			clearMagazineCargo _box;
			clearWeaponCargo _box;
			{
				_type = _x select 0;
				_name = _x select 1;
				_count = _x select 2;
				If (_type == 1) then {
					_box addWeaponCargo [_name,_count];
				} else {
					_box addMagazineCargo [_name,_count];
				};
			} foreach _content;
		};
	};
};

while {true} do {
	_box_idx = 0;
	_not_found = 0;
	while {_not_found < 50} do {
		_box_marker = format["box_%1",_box_idx];
		if (((getMarkerPos _box_marker) distance [0,0,0]) > 0) then {
			_text_marker = markerText _box_marker;
			_marker_array = [];
			if (!isnil "_text_marker") then {
				if (_text_marker != "") then {
					call compile format["_marker_array = %1",_text_marker];
				};
			};
			if (isnil "_marker_array") then {_marker_array = [];};
			if (typename _marker_array != typename []) then {_marker_array = [];};
			_index = AMMO_BOXES_INDEXER find _box_marker;
			_box = objNull;
			_box_array = [];
			_box_type = d_box_type;
			_box_side = d_own_side;
			_box_extra = [];
			if (typename _marker_array == typename []) then {
				if (count _marker_array > 0) then { _box_side = toupper(_marker_array select 0);};
				if (count _marker_array > 1) then { _box_type = _marker_array select 1;};
				if (count _marker_array > 2) then { _box_extra = _marker_array select 2;};
			};
			if (_box_type == "") then { _box_type = d_box_type; };
			if (_index < 0) then {
				_index = count AMMO_BOXES_INDEXER;
				diag_log format["Ammo_Box_Fill: New box: %1", [_index, _box_marker, _marker_array]];
				AMMO_BOXES_INDEXER set [_index, _box_marker];
				_box = _box_type createVehicleLocal (getMarkerPos _box_marker);
				_box setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
				_box_array set [0, _box];
				_box_array set [1, _box_type];
				_box_array set [2, _box_side];
				AMMO_BOXES set [_index, _box_array];
				[_box, _box_side,_box_extra] call _fnc_spawn_content;
				_box setVariable ["Last_Box_Fill",time];
				_marker_name = format ["%1_local",_box_marker];
				[_marker_name, getMarkerPos _box_marker, "ICON", "ColorBlue", [0.6,0.6],"",0,"Pickup","",0.7] call XfCreateMarkerLocal;
				player reveal _box;
			} else {
				if (d_ammobox_refill) then {
					_box_array = AMMO_BOXES select _index;
					_box = _box_array select 0;
					_box_side = _box_array select 2;
					_last_fill = _box getVariable "Last_Box_Fill";
					if (isnil "_last_fill") then { _last_fill = 0; };
					if ((player distance _box) > 200 && (time - _last_fill) > 900) then {
						[_box, _box_side,_box_extra] call _fnc_spawn_content;
						_box setVariable ["Last_Box_Fill",time];
					};
				};
			};
		} else {
			_not_found = _not_found + 1;
		};
		_box_idx = _box_idx + 1;
	};
	for [{_box_idx = 0},{_box_idx < count AMMO_BOXES_INDEXER},{_box_idx = _box_idx + 1}] do {
		_box_marker = AMMO_BOXES_INDEXER select _box_idx;
		if (((getMarkerPos _box_marker) distance [0,0,0]) <= 0) then {
			_box_array = AMMO_BOXES select _box_idx;
			deleteVehicle (_box_array select 0);
			AMMO_BOXES set [_box_idx, "XxDeLxX"];
			AMMO_BOXES_INDEXER set [_box_idx, "XxDeLxX"];
			_marker_name = format ["%1_local",_box_marker];
			deleteMarkerLocal _marker_name;
		};
	};
	AMMO_BOXES_INDEXER = AMMO_BOXES_INDEXER - ["XxDeLxX"];
	AMMO_BOXES = AMMO_BOXES - ["XxDeLxX"];
	sleep 10;
};
